package my.sweep_tchap;
/**
 *
 * @author Tyler Chapman
 * @version 0.1
 * 
 * The user is sent to this page from the MapPage if they wish to enter in 
 * their own street rather than selecting one of the streets that the 
 * MapPage finds for them. If the street they enter is valid they will 
 * be sent to the AlarmPage.
 * 
 */

import org.xmlvm.iphone.CGRect;
import org.xmlvm.iphone.UIButton;
import org.xmlvm.iphone.UIButtonType;
import org.xmlvm.iphone.UIColor;
import org.xmlvm.iphone.UIControl;
import org.xmlvm.iphone.UIControlDelegate;
import org.xmlvm.iphone.UIControlEvent;
import org.xmlvm.iphone.UIControlState;
import org.xmlvm.iphone.UIReturnKeyType;
import org.xmlvm.iphone.UITextBorderStyle;
import org.xmlvm.iphone.UITextField;
import org.xmlvm.iphone.UIView;
import org.xmlvm.iphone.UIViewAutoresizing;
import org.xmlvm.iphone.UIViewController;

public class StreetInput extends UIViewController{
    
	private Integer streetCode=0;
	private String streetName = "";
    
	public void loadView() {
		super.loadView();
		UIView root = getView();
        root.setBackgroundColor(UIColor.groupTableViewBackgroundColor);
		final UITextField editStreet = new UITextField(new CGRect(10, 50, 300, 30));
		editStreet.setAutoresizingMask(UIViewAutoresizing.FlexibleWidth);
		editStreet.setBorderStyle(UITextBorderStyle.Bezel);
		editStreet.setBackgroundColor(UIColor.whiteColor);
		editStreet.setPlaceholder("Your Current Address");
		editStreet.setEnablesReturnKeyAutomatically(true);
		editStreet.setText(streetName);
		editStreet.setReturnKeyType(UIReturnKeyType.Done);
		root.addSubview(editStreet);
		
		UIButton button = UIButton.buttonWithType(UIButtonType.RoundedRect);
		button.setFrame(new CGRect(40, 100, 240, 30));
		button.setTitle("Get Times", UIControlState.Normal);
		button.addTarget(new UIControlDelegate() {
			public void raiseEvent(UIControl uic, int i) {
				/**
				 * Once this button is selected a call is made to the database checking if the street is valid.
				 * If valid the AlarmPage is pushed onto the stack and if not ErrorPage is pushed.
				 */
				streetName = editStreet.getText();
				connect dbConnection = new connect();
				streetCode = dbConnection.searchStreetCode(streetName);
				if(streetCode == 0){
        			getNavigationController().pushViewController(new ErrorPage(), true);
        		}else{
        			getNavigationController().pushViewController(new AlarmPage(streetCode), true);
        		}			}
		}, UIControlEvent.TouchUpInside);
		root.addSubview(button);
		
		setView(root);
	}
}

